package topin.facecat.FaceCat.Product.Script

import facecat.topin.chart.FCScript
import facecat.topin.core.FCTran
import facecat.topin.core.RefObject
import facecat.topin.xml.FCUIScript
import facecat.topin.xml.FCUIXml


class FaceCatScript /*
    创建脚本
     */(private var m_xml: FCUIXml) : FCUIScript {
    /**
     * 析构方法
     *
     */
    @Throws(Throwable::class)
    protected fun finalize() {
        delete()
    }

    var m_script: FCScript? = null
    private var m_isDeleted = false

    /**
     * 获取是否被销毁
     *
     */
    override fun isDeleted(): Boolean {
        return m_isDeleted
    }

    /*
    获取或设置
     */
    override fun getXml(): FCUIXml {
        return m_xml
    }

    override fun setXml(value: FCUIXml) {
        m_xml = value
    }

    /*
    调用方法
     */
    override fun callFunction(function: String): String {
        if (m_script != null) {
            if (m_script != null) {
                val cindex = function.indexOf('(')
                val upperName = function.substring(0, cindex).toUpperCase()
                if (m_script!!.m_tempFunctions.containsKey(upperName)) {
                    val value = m_script!!.callFunction(function)
                    if (m_script!!.m_resultVar != null) {
                        val resultVar = m_script!!.m_resultVar
                        if (resultVar != null && resultVar.m_type == 1) {
                            return if (resultVar.m_str.isNotEmpty() && resultVar.m_str.indexOf("'") == 0) {
                                resultVar.m_str.substring(1, resultVar.m_str.length - 1)
                            } else {
                                resultVar.m_str
                            }
                        }
                    }
                    return FCTran.doubleToStr(value)
                }
            }
        }
        val script: FCScript = NFunctionEx.createIndicator(function, m_xml)
        script.delete()
        return ""
    }

    /**
     * 销毁方法
     *
     */
    fun delete() {
        if (!m_isDeleted) {
            if (m_script != null) {
                m_script!!.delete()
            }
            m_isDeleted = true
        }
    }

    /*
    获取属性
     */
    fun getAttribute(name: String?, propertyName: String?): String? {
        if (m_xml != null) {
            val control = m_xml.findView(name)
            if (control != null) {
                var value: String? = null
                var type: String? = null
                val refValue = RefObject(value)
                val refType = RefObject(type)
                control.getAttribute(propertyName, refValue, refType)
                value = refValue.argvalue
                type = refType.argvalue
                return value
            }
        }
        return null
    }

    /*
    获取调用者
     */
    val sender: String?
        get() {
            if (m_xml != null) {
                val uiEvent = m_xml.event
                if (uiEvent != null) {
                    return uiEvent.sender
                }
            }
            return null
        }

    /*
    设置属性
     */
    fun setAttribute(
        name: String?,
        propertyName: String?,
        propertyValue: String?
    ) {
        if (m_xml != null) {
            val control = m_xml.findView(name)
            control?.setAttribute(propertyName, propertyValue)
        }
    }

    /*
    设置脚本
     */
    override fun setText(text: String) {
        m_script = NFunctionEx.createIndicator(text, m_xml)
    }

}